package org.jeecg.modules.business.third.strategy.sap;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.basic.plant.service.IWmsSapWarehouseMapService;
import org.jeecg.modules.business.third.entity.WmsInReceiptMaterialVo;
import org.jeecg.modules.business.warehouse.entity.WmsInReceiptTaskMaterial;
import org.jeecg.modules.business.warehouse.service.IWmsInReceiptTaskMaterialService;
import org.jeecg.modules.dock.entity.dto.request.OutsourcingPurchaseOrderRequestDTO;
import org.jeecg.modules.dock.entity.dto.request.PurchaseReceiptFinishProductRequestDTO;
import org.jeecg.modules.dock.entity.dto.response.PurchaseReceiptFinishProductResponseDTO;
import org.jeecg.modules.dock.service.SapDockingRepeaterService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/*
 * 项目名称:    wms-server-jeecg
 * 接口描述:    采购入库(原卷) 入库：WMS——>SAP【接口：A807】" R01
 * 创建人员:    zhaojun
 * 创建时间:    2023-10-23
 */
@Service("r49SapWarehouse")
@Slf4j
@RequiredArgsConstructor
public class R49SapWarehouse implements SapOperateStrategy {

    private final IWmsInReceiptTaskMaterialService wmsInReceiptTaskMaterialService;
    private final IWmsSapWarehouseMapService wmsSapWarehouseMapService;
    private final SapDockingRepeaterService sapDockingRepeaterService;

    @Override
    public List operate(Map<String, Object> map) {
        log.info("采购入库(贴心) 入库过账");
        List<PurchaseReceiptFinishProductRequestDTO> list = new ArrayList<>();
        List<WmsInReceiptMaterialVo> wmsInReceiptMaterialVos = (List<WmsInReceiptMaterialVo>) map.get("records");
        wmsInReceiptMaterialVos.forEach(t -> {
            PurchaseReceiptFinishProductRequestDTO dto = new PurchaseReceiptFinishProductRequestDTO();

            SapParamUtil.validWmsInReceiptMaterial(t);

            WmsInReceiptTaskMaterial one = wmsInReceiptTaskMaterialService.getOne(new LambdaQueryWrapper<WmsInReceiptTaskMaterial>().eq(WmsInReceiptTaskMaterial::getOriginalSystemNumber, t.getOriginalSystemNumber())
                    .eq(WmsInReceiptTaskMaterial::getOriginalSystemLineNumber, t.getOriginalSystemLineNumber()));

            SapParamUtil.validInTaskRecord(one);

            OutsourcingPurchaseOrderRequestDTO outsourcingPurchaseOrderRequestDTO = JSONObject.parseObject(one.getExtraText(), OutsourcingPurchaseOrderRequestDTO.class);
            dto.setBatchNumber(one.getBatchCode());
            dto.setProcurementVoucherCode(t.getOriginalSystemNumber());
            dto.setProcurementVoucherProjectCode(Integer.valueOf(t.getOriginalSystemLineNumber()));
            dto.setUuid(outsourcingPurchaseOrderRequestDTO.getUuid());
            // 查询sap的仓库编码
            dto.setStoreLocation(wmsSapWarehouseMapService.getSapStoreCode(t.getStoreLocation()));
            list.add(dto);
        });
        List<PurchaseReceiptFinishProductResponseDTO> purchaseReceiptFinishProduct = sapDockingRepeaterService.getPurchaseReceiptFinishProduct(list);
        return purchaseReceiptFinishProduct;
    }
}
